package com.heng.test.bloomFilter;

import java.util.Arrays;
import java.util.Random;

public class bloomFilterDemo {

    private final int[] arr;
    private final int n;
    private final int capacity;

    public bloomFilterDemo(int capacity, int n) {
        this.arr = new int[capacity];
        this.capacity = capacity;
        this.n = n;
    }

    private int hash(String s, int i) {
        Random random = new Random();
        random.setSeed(s.hashCode() + i);
        return Math.abs(random.nextInt()) % capacity;
    }

    private void add(String s) {
        for (int i = 0; i < n; i++) {
            arr[hash(s, i)] = 1;
        }
    }

    private boolean contains(String s) {
        for (int i = 0; i < n; i++) {
            if (arr[hash(s, i)] == 0) return false;
        }
        return true;
    }


    public static void main(String[] args) {
        bloomFilterDemo bloomFilterDemo = new bloomFilterDemo(100, 5);
        bloomFilterDemo.add("sas");

        System.out.println(bloomFilterDemo.contains("sas"));
        System.out.println(Arrays.toString(bloomFilterDemo.arr));
        System.out.println(bloomFilterDemo.contains("answer"));

        Random r = new Random();
        r.setSeed(1);
        int i = r.nextInt();
        System.out.println(i);

        r.setSeed(1);
        int j = r.nextInt();
        System.out.println(j);
    }
}
